'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken')

class HomeController extends Controller {
    async login() {//登录
        const { ctx, app } = this;
        const { username, password } = ctx.request.body;
        let data = await app.mysql.get('login', { username, password })
        console.log(data);
        
        if (data) {
            let token = jwt.sign(data.username, 'zy')
            ctx.body = {
                code: 1,
                msg:"登陆成功",
                token,
                data
            }
        } else {
            ctx.body = {
                code: 500,
                msg: "登录失败"
            }
        }
    }
    // auth = (token) => {
    //     try {
    //         let res = jwt.verify(token, 'zy')
    //     } catch (err) {
    //         this.ctx.body = {
    //             code: 500,
    //             err,
    //             msg: "异常"
    //         }
    //         return;
    //     }
    // }

    // async list() {
    //     const { ctx } = this;
    //     const { authorization } = ctx.request.body;
    //     auth(authorization);
    //     ctx.body = {
    //         code: 1,
    //         msg: '成功'
    //     }
    // }
}

module.exports = HomeController;
